﻿package  
{
	import com.event.EventZheng;
	import com.utils.DebugTrace;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author zhengzihua
	 */
	public class Ball extends Sprite
	{
		private var sp:Point;//小球起点
		private var ep:Point;//小球终点
		private var timer:Timer=new Timer(50,0);
		public function Ball() 
		{
		}
		public function moveBall(sp:Point, ep:Point):void {
			this.sp = sp;
			this.ep = ep;
			this.x = sp.x;
			this.y = sp.y;
			timer.addEventListener(TimerEvent.TIMER, toCountPoint);
			if (!sp.equals(ep)) {
				timer.start();
			}else{
			}
		}
		private function toCountPoint(e:TimerEvent):void {
			var tempx:int = (ep.x - this.x) * StaticInfo.BALLSPEED;
			var tempy:int = (ep.y - this.y) * StaticInfo.BALLSPEED;
			move(tempx + x, tempy + y);
		}
		private function move(x:int, y:int):void {
			if (Math.abs(ep.x -x) < 2 && Math.abs(ep.y - y) < 2) {
				stopBall()
				return;	
			}
			this.x = x;
			this.y = y;
			var eventZheng:EventZheng = new EventZheng(StaticInfo.MOVEBALL);
			eventZheng.obj.ep = new Point(x, y);
			dispatchEvent(eventZheng);
		}
		public function stopBall():void {
			timer.stop();
			this.x = ep.x;
			this.y = ep.y;
			myDispatchEvent();
		}
		private function myDispatchEvent():void {
			var event:EventZheng = new EventZheng(StaticInfo.STOPBALL);
			event.obj.ep = ep;
			dispatchEvent(event);
			//DebugTrace.dtrace("code info Ball.as:到达目的点.");
		}
	}
	
}